package com.sinodata.bsm.center.web.spi;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sinodata.bsm.center.cache.ResCache;
import com.sinodata.bsm.center.service.resource.ResPauseService;
import com.sinodata.bsm.center.util.spring.SpringContextHolder;
import com.sinodata.bsm.common.vo.Res;
import com.sinodata.bsm.common.vo.ResPauseRule;

/**
 * <p>
 * Description: bsm对外部系统提供的资源暂停接口。外部系统需要http协议，提供资源ID，资源暂停的开始时间和资源暂停的结束时间
 * 
 * </p>
 *
 * @author wangpeng
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2013-4-28 上午11:28:19          wangpeng        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class ResPauseInterfaceServlet extends HttpServlet {

    private static ResPauseService resPauseService = SpringContextHolder.getBean(ResPauseService.class);
    private static ResCache resCache = SpringContextHolder.getBean(ResCache.class);

    private static final long serialVersionUID = 1L;

    @Override
    public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String code = request.getParameter("id");
        String startTimeStr = request.getParameter("startTime");
        String endTimeStr = request.getParameter("endTime");
        if (code == null || code.trim().equals("")) {
            out.println("统一接口ID错误，资源暂停失败");
            return;
        }
        if (startTimeStr == null) {
            out.println("资源暂停开始时间为空，资源暂停失败");
            return;
        }
        if (endTimeStr == null) {
            out.println("资源暂停结束时间为空，资源暂停失败");
            return;
        }
        String[] codes = code.split(",");
        long startTimeLong;
        long endTimeLong;
        try {
            Date startTime = format.parse(startTimeStr);
            Date endTime = format.parse(endTimeStr);
            startTimeLong = startTime.getTime();
            endTimeLong = endTime.getTime();
        } catch (ParseException e) {
            out.println("资源暂停开始时间或结束时间格式错误，资源暂停失败");
            return;
        }
        List<Res> resList = resCache.findAll();
        boolean flag = false;
        for (String c : codes) {
            for (int i = resList.size() - 1; i >= 0; i--) {
                Res res = resList.get(i);
                if (c.equals(res.getCode())) {
                    if (!flag) {
                        flag = true;
                    }
                    ResPauseRule resPauseRule = new ResPauseRule();
                    resPauseRule.setResId(res.getId());
                    resPauseRule.setStatus(1);
                    resPauseRule.setType(2);//周期暂停
                    resPauseRule.setRule(startTimeLong + "@" + endTimeLong);
                    resPauseService.saveOrUpdateResPauseRule(resPauseRule);
                }
                resList.remove(i);
            }
        }
        if (flag) {
            out.println("资源暂停成功");
        } else {
            out.println("没有与给定ID匹配的资源");
        }
    }
}
